/*
 * Class FollowPlayer
 *
 * Version 1.0
 *
 * Sunday, Semptember 7th, 2008
 *
 * Created by NoobScape
 */

package net.com.hydroPK.model;

import net.com.hydroPK.Engine;

public class PlayerFollow {
    public int WalkingTo;
    public int followPlayer;

    public void followPlayer(Player p) {


        Player p2 = Engine.players[p.playerId + 32768];
        int n;
        n = p2.absY - p.absY; // This will get updated in process.

        if ((p2.absX - p.absX) <= (n * (-1)) && (p2.absX - p.absX) >= (n - 1)) {
            if (n < 0) {
                p.WalkingTo(p.absX - 1, p.absY);
            }
            if (n > 0) {
                p.WalkingTo(p.absX, p2.absY - 1);
            }
        } else if ((p2.absX - p.absX) > 0) {
            p.WalkingTo(p2.absX - 1, p2.absY);
        } else if ((p2.absX - p.absX) < 0) {
            p.WalkingTo(p2.absX + 1, p2.absY);
        }

        if (((p2.absX - p.absX) == 0) && (n < 0)) {
            p.WalkingTo(p2.absX, p2.absY + 1);
        }
        if (((p2.absX - p.absX) == 0) && (n >= 0)) {
            p.WalkingTo(p2.absX, p2.absY - 1);
        }
        if (p2.absX - p.absX > 12 || p.absX - p2.absX > 12 || p2.absY - p.absY > 12 || p.absY - p2.absY > 12) {
            p.WalkingTo(p.absX, p.absY); // Reset walking if the other player is further then 12 squares
            // (To prevent the following if p2 teleports away)
        }
        p.requestFaceTo(p2.playerId + 32768);
    }

    public void resetFollow(Player p) {
        if (p == null) {
            return;
        }
        p.followingPlayer = false;
    }
}